﻿Imports AugmentLib.Database
Imports System.Configuration.ConfigurationManager
Imports AugmentLib.InputOutput.Encryption

Partial Class RenReportMore
    Inherits System.Web.UI.Page

#Region " Properties "

    Public Property CurrentPage() As Integer
        Get
            'Looks for the current page in ViewState
            Dim objPage As Object = ViewState("_CurrentPage")
            If objPage Is Nothing Then
                Return 0
            Else
                'default to showing the first page
                Return CInt(objPage)
            End If
        End Get
        Set(ByVal value As Integer)
            ViewState("_CurrentPage") = value
        End Set
    End Property

    Public Property PageSize() As Integer
        Get
            'Looks for the current page in ViewState
            Dim objPageSize As Object = ViewState("_PageSize")
            If objPageSize Is Nothing Then
                Return 10
            Else
                'default to showing the first page
                Return CInt(objPageSize)
            End If
        End Get
        Set(ByVal value As Integer)
            ViewState("_PageSize") = value
        End Set
    End Property

#End Region

#Region " Paging and Page Size "

    Protected Sub lnkHead10Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead10Item.Click
        PageSize = 10
        LoadRenReportByDate()
    End Sub

    Protected Sub lnkHead25Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead25Item.Click
        PageSize = 25
        LoadRenReportByDate()
    End Sub

    Protected Sub lnkHead100Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead100Item.Click
        PageSize = 100
        LoadRenReportByDate()
    End Sub

    Protected Sub lnkHead150Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead150Item.Click
        PageSize = 150
        LoadRenReportByDate()
    End Sub

    Protected Sub lnkFoot10Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot10Item.Click
        PageSize = 10
        LoadRenReportByDate()
    End Sub

    Protected Sub lnkFoot25Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot25Item.Click
        PageSize = 25
        LoadRenReportByDate()
    End Sub

    Protected Sub lnkFoot100Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot100Item.Click
        PageSize = 100
        LoadRenReportByDate()
    End Sub

    Protected Sub lnkFoot150Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot150Item.Click
        PageSize = 150
        LoadRenReportByDate()
    End Sub

    Protected Sub btnMoveFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveFirst.Click
        CurrentPage = 0
        txtPageNo.Text = CurrentPage + 1
        LoadRenReportByDate()
    End Sub

    Protected Sub btnPrevious_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        If CurrentPage > 0 Then
            CurrentPage = CurrentPage - 1
            txtPageNo.Text = CurrentPage + 1
            LoadRenReportByDate()
        End If
    End Sub

    Protected Sub btnGo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGo.Click
        If CInt(txtPageNo.Text < 1) Then
            txtPageNo.Text = 1
        End If

        If CInt(txtPageNo.Text > CInt(lblMaxPageCount.Text)) Then
            txtPageNo.Text = CInt(lblMaxPageCount.Text) + 1
        End If

        CurrentPage = CInt(txtPageNo.Text) - 1
        LoadRenReportByDate()

    End Sub

    Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click
        If CurrentPage < CInt(lblMaxPageCount.Text) Then
            CurrentPage = CurrentPage + 1
            txtPageNo.Text = CurrentPage + 1
            LoadRenReportByDate()
        End If
    End Sub

    Protected Sub btnMoveLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveLast.Click
        CurrentPage = CInt(lblMaxPageCount.Text)
        txtPageNo.Text = CurrentPage + 1
        LoadRenReportByDate()
    End Sub

#End Region

    Private Sub LoadRenReportByDate()
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim TempDT As New Data.DataTable

        Dim dd, mm, yy As String
        Dim dt As DateTime = DateTime.ParseExact(lblDate.Text, "dd-MMM-yyyy", Nothing)
        dd = Format(dt, "dd")
        mm = Format(dt, "MM")
        yy = Format(dt, "yyyy")

        objDB.OpenConnection()
        objDB.dbVariable.SQLString = "SELECT RR.Title, CASE RR.RenReportType WHEN '1' THEN 'Report Entry' WHEN '2' " & _
                                    "THEN 'Reminder' ELSE '' END as RenReportType, RC.Name, RR.RenReportId FROM AW_RenReport RR LEFT JOIN " & _
                                    "AW_RenContact RC ON RR.RenContactId=RC.RenContactId WHERE RR.RenDate=@RenDate " & _
                                    "AND RR.RenMonth=@RenMonth AND RR.RenYear=@RenYear AND RR.CreateBy=@CreateBy " & lblhidSortType.Text
        objDB.AddParameter("RenDate", Data.SqlDbType.TinyInt, dd)
        objDB.AddParameter("RenMonth", Data.SqlDbType.TinyInt, mm)
        objDB.AddParameter("RenYear", Data.SqlDbType.Int, yy)
        objDB.AddParameter("CreateBy", Data.SqlDbType.UniqueIdentifier, New Guid(lblStaffUID.text))
        objDB.FillData(TempDT, "AW_RenReportMore", Database.SQLCommandType.NormalString)

        objDB.CloseConnection()

        'Creating a Paged Datasouce
        Dim objPaged As New PagedDataSource
        objPaged.DataSource = TempDT.DefaultView
        objPaged.AllowPaging = True
        objPaged.PageSize = PageSize
        objPaged.CurrentPageIndex = CurrentPage

        lblHeadNoOfItem.Text = TempDT.Rows.Count
        lblFootNoOfItem.Text = TempDT.Rows.Count

        repRenReportMore.DataSource = objPaged
        repRenReportMore.DataBind()

        lblMaxPageCount.Text = objPaged.PageCount - 1
        lblTotalPage.Text = " of " & objPaged.PageCount
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session.Item("StaffId") Is Nothing Then
            Response.Redirect("default.aspx")
        End If

        If Not Page.IsPostBack Then

            lblStaffUID.Text = Session.Item("StaffId").ToString
            lblStaffType.Text = Session.Item("StaffType").ToString

            Dim dt As DateTime = DateTime.ParseExact(SymmetricDecryptFromHex(Request.QueryString("date"), "augment"), "d/M/yyyy", Nothing)
            lblDate.Text = Format(dt, "dd-MMM-yyyy")
            LoadRenReportByDate()
        End If

        If Not Session("Back") Is Nothing Then
            Dim strBack As String = Session("Back").ToString
            Dim arr() As String = strBack.Split("ڡ")
            If arr(0).ToString.Contains("renreportmore.aspx") Then
                PageSize = CInt(arr(1).ToString)
                CurrentPage = CInt(arr(2).ToString)
                lblDate.Text = DateTime.ParseExact(arr(3), "dd-MMM-yyyy", Nothing)
                Session.Remove("Back")
                LoadRenReportByDate()
            Else
                Session.Remove("Back")
            End If
          
        End If

    End Sub

    Protected Sub imgNewRenReport_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgNewRenReport.Click
        Response.Redirect("RenReportEntry.aspx?date=" & SymmetricEncryptToHex(Format(DateTime.ParseExact(lblDate.Text, "dd-MMM-yyyy", Nothing), "d/M/yyyy"), "augment"))
    End Sub


    Protected Sub repRenReportMore_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles repRenReportMore.ItemCommand
        If e.CommandName.ToUpper = "EDITITEM" Then
            Dim strBack As String
            strBack = "renreportmore.aspx" & "ڡ" & PageSize & "ڡ" & CurrentPage & lblDate.Text.Trim
            Session.Add("Back", strBack)
            Dim RenReportUID As String = CType(repRenReportMore.Items(e.CommandArgument).FindControl("lblRepRenReportUID"), Label).Text
            Response.Redirect("RenReportEntry.aspx?id=" & SymmetricEncryptToHex(RenReportUID, "augment"))
        Else
            'Begin Delete
            Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))

            objDB.OpenConnection()
            Try
                objDB.InitDbTrans()
                Dim RenReportUID As String = CType(repRenReportMore.Items(e.CommandArgument).FindControl("lblRepRenReportUID"), Label).Text
                objDB.dbVariable.SQLString = "UPDATE AW_RenReport SET UpdateBy=@UpdateBy, UpdateDate=@UpdateDate, StatusDelete=1 " & _
                                        "WHERE RenReportId=@RenReportId"
                objDB.AddParameter("UpdateBy", Data.SqlDbType.UniqueIdentifier, New Guid(lblStaffUID.Text.Trim))
                objDB.AddParameter("UpdateDate", Data.SqlDbType.DateTime, Now())
                objDB.AddParameter("RenReportId", Data.SqlDbType.UniqueIdentifier, New Guid(RenReportUID))
                objDB.ExecuteQuery()
                objDB.DbTransCommit()
                objDB.CloseConnection()
                lblMsgSuccessful.Text = "Ren Report Deleted"
                lblMsgSuccessful.Visible = True
                LoadRenReportByDate()
            Catch ex As Exception
                lblMsgError.Text = ex.Message
                divError.Visible = True
                If objDB.dbVariable.objConn.State = Data.ConnectionState.Open Then
                    objDB.DbTransRollback()
                    objDB.CloseConnection()
                End If
            End Try
        End If
    End Sub

    Private Function SortType(ByVal strSortType As String) As String
        Dim strSort As String
        If Right(strSortType, 1) = "▼" Then
            strSort = "ASC"
        Else
            strSort = "DESC"
        End If
        Return strSort
    End Function

    Protected Sub LnkName_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LnkName.Click
        If LnkName.Text = "Name▼" Then
            LnkName.Text = "Name▲"
        Else
            LnkName.Text = "Name▼"
        End If

        lblhidSortType.Text = " ORDER BY Name " & SortType(LnkName.Text) & ", Type " & SortType(lnkType.Text) & _
                            ", Title " & SortType(lnkTitle.Text)
        LoadRenReportByDate()
    End Sub

    Protected Sub lnkTitle_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkTitle.Click
        If lnkTitle.Text = "Title▼" Then
            lnkTitle.Text = "Title▲"
        Else
            lnkTitle.Text = "Title▼"
        End If

        lblhidSortType.Text = " ORDER BY Title " & SortType(lnkTitle.Text) & ", Type " & SortType(lnkType.Text) & _
                            ", Name " & SortType(LnkName.Text)
        LoadRenReportByDate()
    End Sub

    Protected Sub lnkType_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkType.Click
        If lnkType.Text = "Type▼" Then
            lnkType.Text = "Type▲"
        Else
            lnkType.Text = "Type▼"
        End If

        lblhidSortType.Text = " ORDER BY Type " & SortType(lnkType.Text) & ", Title " & SortType(lnkTitle.Text) & _
                            ", Name " & SortType(LnkName.Text)
        LoadRenReportByDate()
    End Sub
End Class
